<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入角色名称">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4"><el-button type="primary">添加角色</el-button></el-col>
      </el-row>
      <el-table :data="roleList" style="width: 100%" stripe>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-row  :class="['v-center', i1 === 0 ? 'bd-top' : '', 'bd-bottom']" v-for="(i1,index) in props.row.children" :key="index">
              <el-col :span="6">
                <el-tag closable @close="delRights(props.row, i1)">{{ i1.name }}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <el-col :span="20">
                <el-row :class="['v-center', i2 === 0 ? '' : 'bd-top']" v-for="(i2,index) in i1.children" :key="index">
                  <el-col :span="6">
                    <el-tag type="warning" closable @close="delRights(props.row, i2)">{{ i2.name }}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag @close="delRights(props.row, i3)" type="warning" closable v-for="(i3,index) in i2.children" :key="index">{{ i3.name }}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="角色名称" width="180"></el-table-column>
        <el-table-column prop="remark" label="角色描述" width="500"></el-table-column>
        <el-table-column label="操作" width="290">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-delete" size="small">编辑</el-button>
            <el-button type="danger" icon="el-icon-delete" size="small">删除</el-button>
            <el-button type="warning" icon="el-icon-setting" size="small" @click="openAssignRight(scope.row)">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog title="分配权限" :visible.sync="rightDialogVisible" width="40%">
      <el-tree :data="rights" show-checkbox  default-expand-all node-key="id"
               :default-checked-keys="keys" ref="tree" highlight-current :props="defaultProps">
      </el-tree>
      <span slot="footer" class="dialog-footer">
    <el-button @click="rightDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="assignRight">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'Roles',
  data () {
    return {
      roleId: '',
      keys: [],
      roleList: [],
      rightDialogVisible: false, // 分配权限提示框是否显示
      rights: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      }
    }
  },
  created () {
    this.getRoleList()
    this.getRights()
  },
  methods: {
    // 完成赋权操作
    async assignRight () {
      const rights = [...this.$refs.tree.getCheckedKeys(),
        ...this.$refs.tree.getHalfCheckedKeys()]
      const roleId = this.roleId
      const rolRes = rights.map(resId => {
        return {
          roleId, resId
        }
      })
      const { data: res } = await this.$http.put('sys/roleRes/' + roleId, rolRes)
      if (res.code !== 200) { this.$message.error(res.msg) }
      this.$message.success(res.msg)
      await this.getRoleList()
      this.rightDialogVisible = false
    },
    // 获取所有权限节点
    async getRights () {
      const { data: res } = await this.$http.get('sys/res')
      if (res.code !== 200) { return this.$mesage.error(res.msg) }
      this.rights = res.data
    },
    LeafKey (right, arr) {
      if (right.children.length === 0) {
        arr.push(right.id)
      }
      right.children.forEach(item => this.LeafKey(item, arr))
    },
    // 打开分配权限框
    openAssignRight (role) {
      this.rightDialogVisible = true
      const arr = []
      this.keys = []
      role.children.forEach(item => this.LeafKey(item, arr))
      this.keys = arr
      this.roleId = role.id
    },
    async getRoleList () {
      const { data: res } = await this.$http.get('sys/role')
      if (res.code !== 200) {
        return this.$message.error(res.msg)
      } else {
        this.roleList = res.data.data
      }
    },
    // 删除权限节点
    delRights (role, rights) {
      this.$confirm('此操作将删除此权限，是否继续？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const arr = []
        this.findChildId(rights, arr)
        const { data: res } = await this.$http.delete('sys/roleRes/' + role.id + '/' + arr.join(','))
        if (res.code !== 200) { return this.$message.error(res.msg()) }
        await this.getRoleList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      })
    },
    // 查询权限子节点
    findChildId (right, arr) {
      if (right.children.length !== 0) {
        right.children.forEach(item => this.findChildId(item, arr))
      }
      arr.push(right.id)
    },
    getCheckedKeys () {
      console.log(...this.$refs.tree.getCheckedKeys())
      console.log(...this.$refs.tree.getHalfCheckedKeys())
    },
    setCheckedKeys () {
      this.$refs.tree.setCheckedKeys([1])
    },
    resetChecked () {
      this.$refs.tree.setCheckedKeys([])
    }
  }
}
</script>

<style scoped>
  .el-tag{
    margin: 7px;
  }
  .v-center{
    display: flex;
    align-items: center;
  }
  .bd-top{
    border-top: 1px solid #ccc;
  }
  .bd-bottom{
    border-bottom: 1px solid #ccc;
  }
</style>
